<template>
  <el-dialog :title="title" class="addSP" width="400px" :visible.sync="isDial" :before-close='closeDialog'>
    <el-form :model="form" size="mini" ref="addSP" :rules="rules">
      <el-form-item label="参数父类" :label-width="formLabelWidth">
        <el-input v-model="form.parentClass" autocomplete="off" :disabled="true"></el-input>
      </el-form-item>
      <el-form-item label="项目序号" prop='sort' :label-width="formLabelWidth">
        <el-input v-model="form.sort" :maxlength='5' autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="项目编码" prop='value' :label-width="formLabelWidth">
        <el-input v-model="form.value" class="leftBorder" :maxlength='10' autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="项目值" prop='code' :label-width="formLabelWidth">
        <el-input v-model="form.code" :maxlength='50'  class="leftBorder" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop='remark' :label-width="formLabelWidth">
        <el-input type="textarea" :maxlength='100' v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="addSP" size="mini">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { request } from "../../assets/request.js";
import {checkClass} from "../../assets/javascript/utils/checkClass.js";
export default {
  data() {
    return {
      isDial: false,
      form: {
        parentClass: "",
        sort: "0",
        value: "",
        code: "",
        remark: ""
      },
      rules: {
        code: [
          {
            pattern: /^[\s\S]*.*[^\s][\s\S]*$/,
            required: true,
            message: "项目值是必填项",
            trigger: "blur"
          }
        ],
        value: [
          {
            pattern: /^[\s\S]*.*[^\s][\s\S]*$/,
            required: true,
            message: "项目编码是必填项",
            trigger: "blur"
          }
        ]
      },
      formLabelWidth: "80px",
      title: "添加参数",
      actData: ""
    };
  },
  created() {
    this.$root.$on("addSP", (type, data) => {
      this.isDial = true;
      let form = this.form;
      let actData = this.actData;
      form.parentClass = data.code;
      this.actData = data;
      if (type == "edit") {
        this.title = "编辑参数";
        let url = "constant/" + data.id;
        let that = this; //解决this指向问题
        request.ajaxGet({
          url,
          that,
          fn(res) {
            if (res) {
              let form = that.form;
              form.sort = res.sort;
              form.remark = res.remark;
              form.code = res.code;
              form.value = res.value;
              form.id=res.id
            }
          }
        });
      } else if(type =='addF'){
        form.parentClass = '父类参数';
        this.title = "添加父参数";
        let obj ={id:0}
        this.actData = obj
      }else{
        this.title = "添加参数";
      }
    });
  },
  methods: {  
    addSP() {
        this.$refs.addSP.validate(valid => {
             if(!valid){
                return
            }
            let that = this;
            let url = "constant";
            let form = this.form;
            if(form.sort){
              if(!checkClass.checkInteger(form.sort)){
                that.$message({message: "项目序号只能是整数或小数",type: "warning"});
                return
              }
            }
            let actData = this.actData;
            let params = {
                code: form.code,
                parentId: actData.id,
                value: form.value,
                remark: form.remark,
                sort:parseInt(form.sort)
            };
            let met='POST'
            if (that.title == "编辑参数") {
                met = "PUT";
                params.id = form.id;
                params.parentId=actData.parentId
            }
            request.ajaxPost({
                url,
                that,
                type: met,
                params,
                fn(res) {
                that.isDial = false;
                that.$root.$emit("saveSP",'no')
                that.resetForm();
                that.$store.state.positionList=''
                }
            });
        })
    },
    closeDialog(){
      this.isDial=false
      this.resetForm();
    },
    resetForm() {
      this.$refs['addSP'].resetFields();
    },
  }
};
</script>
<style lang="less" scoped>
.addSP {
  text-align: left;
}
</style>

